<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="所属公司" prop="companyId">
        <el-tree-select
          v-model="queryParams.companyId"
          :data="companyList"
          :render-after-expand="true"
          :props="treeProps"
          node-key="id"
          clearable check-strictly
        />
      </el-form-item>
      <!-- <el-form-item label="司机编号" prop="driverNo">
        <el-input
          v-model="queryParams.driverNo"
          placeholder="请输入司机编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="司机姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="item in bus_line_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['bus:driver:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['bus:driver:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="driverList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="所属公司" min-width="150" align="center" prop="companyName" />
        <el-table-column label="车辆编号" align="center" prop="busNo" />
        <el-table-column label="司机编号" align="center" prop="driverNo" />
        <el-table-column label="司机姓名" align="center" prop="name" />
        <el-table-column label="司机性别" width="80" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :options="bus_driver_sex" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="phone" min-width="120" />
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              inline-prompt
              :active-value="1"
              :inactive-value="2"
              inactive-text="禁用"
              active-text="启用"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" min-width="160" align="center" prop="updateTime" />
        <el-table-column label="操作" width="110" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['bus:driver:query']"></el-button>
            </el-tooltip>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="edit" @click="handleUpdate(scope.row)" v-hasPermi="['bus:driver:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="delete" @click="handleDelete(scope.row)" v-hasPermi="['bus:driver:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog v-model="showDialog" :title="dialogTitle">
      <edit-driver
        v-if="dialogType == 1"
        ref="editDriver"
        :company-list="companyList"
        @cancel="cancel"
        @confirm="confirm"
      />
      <driver-detail
        v-if="dialogType == 2"
        ref="driverDetail"
        @cancel="cancel"
      />
    </Dialog>
  </div>
</template>

<script setup name="Driver">
import { listDriver, delDriver, changeDriverStatus } from "@/api/bus/driver"
import { getAllCompanies } from '@/api/company/company'
import EditDriver from './edit.vue'
import DriverDetail from './detail.vue'
import { nextTick } from "vue"

const editDriver = ref()
const driverDetail = ref()
const { proxy } = getCurrentInstance()
const { bus_line_status, bus_driver_sex } = proxy.useDict("bus_line_status", "bus_driver_sex")

const companyList = ref([])
const driverList = ref([])
const loading = ref(false)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref(0)
const treeProps = {
  label: 'name',
  disabled: (data, node) => {
    return data.check == false
  }
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyId: null,
    driverNo: null,
    name: null,
    phone: null,
    status: null
  }
})

const { queryParams } = toRefs(data)

/** 公司列表 */
function getCompanyList() {
  getAllCompanies().then(res => {
    companyList.value = res.data || []
  })
}

/** 查询公交司机列表 */
function getList() {
  loading.value = true
  listDriver(queryParams.value).then(response => {
    driverList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 详情 */
function handleView(row) {
  showDialog.value = true
  dialogType.value = 2
  dialogTitle.value = '司机详情'
  nextTick(() => {
    driverDetail.value.pageInit(row.id)
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogType.value = 0
  dialogTitle.value = ''
}

const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.status === 1 ? "启用" : "禁用"
  proxy.$modal.confirm('确认要"' + text + '"吗?').then(function () {
    const data = {
      status:row.status,
      id:row.id
    }
    return changeDriverStatus(data)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功")
  }).catch(function () {
    row.status = row.status === 1 ? 2 : 1
  })
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '添加公交司机'
  nextTick(() => {
    editDriver.value.pageInit()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '修改公交司机'
  nextTick(() => {
    editDriver.value.pageInit(_id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  if (row.status == 1) {
    return proxy.$modal.msgWarning('该司机已启用，不可以删除')
  }
  proxy.$modal.confirm('是否确认删除该司机？').then(function() {
    return delDriver(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/driver/export', {
    ...queryParams.value
  }, `司机列表_${proxy.getFileImportName()}.xlsx`)
}

getCompanyList()
getList()
</script>
